package main

import (
	"fmt"
	"net"
	"time"
)

/*
   Author: mahaiyuan
   Date: 2021/4/19 8:01 下午
   Description:
*/
func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:9999")
	if err != nil {
		fmt.Println("conn server fail.", err)
		return
	}
	defer conn.Close()

	for i := 0; i < 100; i++ {

		data := fmt.Sprintf("hello_%d", i)
		_, err := conn.Write([]byte(data)) //向服务器发送数据
		if err != nil {
			fmt.Println("send data fail.", err)
			continue
		}
		bytes := make([]byte, 512)
		n, err := conn.Read(bytes)
		if err != nil {
			fmt.Println("read data from server error.", err)
			continue
		}
		fmt.Println("read data is :", string(bytes[:n]))
		time.Sleep(time.Second)
	}
}
